package com.qf.myblogssm.service.serviceImp;

import com.qf.myblogssm.dao.TarticleMapper;
import com.qf.myblogssm.dao.TtagMapper;
import com.qf.myblogssm.pojo.Tarticle;
import com.qf.myblogssm.pojo.Ttag;
import com.qf.myblogssm.service.ArticleService;
import com.qf.myblogssm.utils.ArticleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by 王镜 on 2019/6/17 0017 14:29
 */
@Service
//给所有方法添加事务
//@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private TarticleMapper tarticleMapper;
    @Autowired
    private TtagMapper ttagMapper;
    @Override
    public List<Tarticle> selectAllArticles() {
        List<Tarticle> tarticles = tarticleMapper.selectAll();
        return  ArticleUtils.dealContent(ArticleUtils.dealTime(tarticles));
        //处理文章内容和处理时间
    //    return tarticles;
    }

    @Override
    public Tarticle getArticleById(Integer id) {
        Tarticle tarticle = tarticleMapper.selectByPrimaryKey(id);
        //需要处理时间和内容
        return ArticleUtils.dealArticleTime(tarticle);
      //  return tarticle;
    }

    @Override
    public Integer countNumber() {
        Integer number=tarticleMapper.countNumber();
        return number;
    }

    @Override
    public Integer countSortNumber() {
        Integer sortNumber=tarticleMapper.countSortNumber();
        return sortNumber;

    }

    @Override
    public List<Tarticle> getArticle_sort_count() {
       List<Tarticle> tarticles= tarticleMapper.getArticle_sort_count();
        return tarticles;
    }

    //当我们需要一个事务的时候，就创建一个事务，当操作失败时，它会回滚
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    //进行事务捕获异常一般在controller层进行操作，如果说一定要在service层捕获异常，一定要抛出一个运行时异常
    public void star(Integer id) {
       // try{
            tarticleMapper.star(id);

            //从域里面把用户拿出来，写一个判断条件，判断用户是否点赞过
         //   int i=1/0;
//        }catch (Exception e){
//            e.printStackTrace();
//            new RuntimeException();
//        }

    }

    @Override
    public Map<String, List<Tarticle>> getSortInfo() {
        //获取每个分类对应的文章
        //获取分类的名称，并且保证其不重复
        //通过分类的名称去获取到对应的文章，此时得到的是一个list
        //再将每个分类对应的list封装成一个map

        List<Tarticle> tarticles=tarticleMapper.selectBySort();
        Map<String,List<Tarticle>> map=new HashMap<>();
        for(Tarticle tarticle:tarticles){
          List<Tarticle> tarticleList= tarticleMapper.selectArticleBySort(tarticle.getSort());
          //封装最后的数据
            map.put(tarticle.getSort(),tarticleList);
        }
        return map;
    }

    @Override
    public List<Tarticle> getBySort(String sort) {
        List<Tarticle> tarticles = tarticleMapper.selectArticleBySort(sort);
        return  ArticleUtils.dealContent(ArticleUtils.dealTime(tarticles));
    }

    @Override
    public List<Tarticle> getByTag(String get) {
        List<Tarticle> tarticles=new ArrayList<>();
        List<Ttag> ttagList=ttagMapper.selectByName(get);
        for(Ttag ttag1:ttagList)
        {
            Tarticle tarticle= tarticleMapper.selectByPrimaryKey(ttag1.getId());
            tarticles.add(tarticle);
        }
        return ArticleUtils.dealContent(ArticleUtils.dealTime(tarticles));
    }

    @Override
    public Tarticle getByPreArticleId(Integer id) {
        List<Tarticle> tarticles=tarticleMapper.selectAll();
        Integer len=tarticles.size();
        Integer m=0;
        for(Tarticle tarticle:tarticles)
        {
            if(tarticle.getId()<id)
            {
                m=tarticle.getId();
            }
        }
        if(m==0)
        {
            return null;
        }
        else {
            Tarticle tarticle = tarticleMapper.selectByPrimaryKey(m);
            return ArticleUtils.dealArticleTime(tarticle);
        }

    }

    @Override
    public Tarticle getByNextArticleId(Integer id) {
        List<Tarticle> tarticles=tarticleMapper.selectAll();
        Integer len=tarticles.size();
        Integer m=0;
        for(Tarticle tarticle:tarticles)
        {
            if(tarticle.getId()>id)
            {
                m=tarticle.getId();
                break;
            }
        }
        if(m==0)
        {
            return null;
        }
        else {
            Tarticle tarticle = tarticleMapper.selectByPrimaryKey(m);
            return ArticleUtils.dealArticleTime(tarticle);
        }
    }


}
